The R library crosstalk is an increasingly popular tool in the realm of web development, especially in the context of data analysis and visualization.
Crosstalk is a JavaScript library for two-way data binding between widgets in HTML, making it easier to create coordinated, interactive visualizations.
Furthermore, crosstalk facilitates synchronization and communication between different HTML widgets or visualizations, ensuring that interactions in one widget affect others.
This library is often used by web developers and data analysts who want to create interactive visualizations that are synchronized across multiple components.
The main advantages of crosstalk are:
Integration Flexibility: crosstalk is beneficial when you wish to integrate interactive features across different JavaScript-based visualization libraries or HTML widgets seamlessly. Moreover, crosstalk can be used within Shiny applications to enhance interactivity between different visual elements;
Coordinated Interactivity: crosstalk is suitable for scenarios where you have multiple visualizations or components that need to be synchronized based on user interactions. For instance, when selecting a data point in one plot should simultaneously highlight or update information in another;
Collaborative Dashboards: crosstalk is applicable when you need to build interactive dashboards where user interactions across different elements (like plots, tables, maps) should influence each other dynamically;
JavaScript-Based Requirements: crosstalk is useful when working within a JavaScript-focused environment or when needing specific interactions that are more efficiently handled with JavaScript.
In today’s post, we will practice the R library crosstalk using a dataset on number of animals slaughtered for meat worldwide retrieved from the website “Our World in Data” (https://ourworldindata.org/animal-welfare). For the sake of simplification, we will retain the top 3 world’s largest meat producers in 2021 and European Union.
Therefore, we will practice the following skills:
## # A tibble: 199 × 4
## Rank Country `Meat production(in tonnes)` Year
## <int> <chr> <chr> <int>
## 1 1 China 90,737,304 2021
## 2 2 United States 48,876,684 2021
## 3 3 Brazil 29,497,016 2021
## 4 4 Russia 11,346,122 2021
## 5 5 India 10,888,240 2021
## 6 6 Mexico 7,692,412 2021
## 7 7 Spain 7,649,473 2021
## 8 8 Germany 7,632,081 2021
## 9 9 Argentina 6,152,519 2021
## 10 10 France 5,363,326 2021
## # ℹ 189 more rows
## # A tibble: 27 × 12
## Country Accession `Population[2]` `Area (km2)` `Largest city` `GDP(US$ M)`
## <chr> <chr> <chr> <chr> <chr> <chr>
## 1 Austria 1 Januar… 8,926,000 83,855 Vienna 447,718
## 2 Belgium March 25… 11,566,041 30,528 Brussels 517,609
## 3 Bulgaria 1 Januar… 6,916,548 110,994 Sofia 66,250
## 4 Croatia 1 July 2… 4,036,355 56,594 Zagreb 60,702
## 5 Cyprus[a] 1 May 20… 896,000 9,251 Nicosia 24,280
## 6 Czech Rep… 1 May 20… 10,574,153 78,866 Prague 246,953
## 7 Denmark[c] 1 Januar… 5,833,883 43,075 Copenhagen 347,176
## 8 Estonia 1 May 20… 1,330,068 45,227 Tallinn 31,038
## 9 Finland[d] 1 Januar… 5,527,493 338,424 Helsinki 269,654
## 10 France[e] March 25… 67,439,614 632,833[7] Paris 2,707,074
## # ℹ 17 more rows
## # ℹ 6 more variables: `GDP (PPP)per cap.[3][4]` <chr>, Currency <chr>,
## # `Gini[5]` <chr>, `HDI[6]` <dbl>, MEPs <int>, `Official languages` <chr>
First, we search for the ranking of world’s largest meat producers. This can be found on https://en.wikipedia.org/wiki/List_of_countries_by_meat_production.
And with that we can start retrieving the data from the selected table.
Initially, all tables present in the webpage will be listed in R.
We then just need to select the table we need.
Thereafter, we can retrieve the names of the top 3 meat producers (China, United States, Brazil) and include them into a vector, which will be used to filter the observations we will use further in this exercise.
Additionally, the European Union can be added as a block country and list of current European Union members can be found here: https://en.wikipedia.org/wiki/Member_state_of_the_European_Union.
The process of retrieving the table and creating a vector with the 27 member states (Austria, Belgium, Bulgaria, Croatia, Cyprus[a], Czech Republic, Denmark[c], Estonia, Finland[d], France[e], Germany, Greece, Hungary, Ireland, Italy, Latvia, Lithuania, Luxembourg, Malta, Netherlands[h], Poland, Portugal[i], Romania, Slovakia, Slovenia, Spain[k], Sweden) follows the same steps described above for the top 3 meat producers.
The data on the number of land animals slaughtered for meat per year between 1961 and 2021 was previously downloaded as csv. format from the “Our World in Data” website (https://ourworldindata.org/animal-welfare).
After uploading it in R, the observations from the 3 selected countries were filtered out in one sub-dataset and the EU member states were filtered out in another sub-dataset and observations across countries were summed per year.
Thereafter, both sub-datasets were merged into one dataset “data” with 244 yearly observations.
Then, the yearly average number of land animals slaughtered was calculated using the R library skimr and displayed in Table 1 using the R library flextable.
Country | Average |
|---|---|
United States | 6,469,932,188 |
China | 6,041,165,848 |
Brazil | 2,680,708,955 |
European Union | 2,618,541,282 |
Our crosstalk app contains:
Selection boxes for “country” and “year” helping you filtering desired rows and information;
Table displaying the absolute number of land animals slaughtered by China, United States, Brazil, and EU between 1961 and 2021;
Figure illustrating the variation in percentage of land animals slaughtered for meat from 1961 to 2021.
You can play with the example below by manipulating the selection boxes, clicking rows in the data table, and playing with the selection button in the figure.
Although this article is about data science skills, we can’t close our eyes to the enormous amount of land animals slaughtered for meat every year worldwide!
Only Brazil, China, USA, and EU slaughter around 37 billion land animals for meat!
Additionally, the evolution in the number of land animals slaughtered for meat between 1961 and 2021 is also impressive, especially in Brazil and China with increases of 4254% and 2730%, respectively.
These sizable figures indicate substantial opportunities to conduct research and improve animal welfare and health!
If you reached this point, you learned web scraping, data loading, handling and processing, and creating a interactive table and plot using the R library crosstalk. Congrats!
Crosstalk is excellent for creating synchronized and interactive HTML widgets.
Similar to standard HTML widgets, Crosstalk operates independently of Shiny or any additional R runtime dependencies.
This independence allows you to employ it either in isolation or in conjunction with R Markdown for producing static HTML documents that are easily hostable on various platforms like Amazon S3, GitHub, or RPubs.
Nevertheless, it’s intentionally built to seamlessly integrate with Shiny applications, enabling the fusion of Crosstalk widget interactions with an array of elements including base plots, ggplot2, and the full spectrum of Shiny functionalities.
See you and enjoy coding!